package com.zr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zr.dao.CheckGroupDao;
import com.zr.entity.PageResult;
import com.zr.entity.QueryPageBean;
import com.zr.pojo.CheckGroup;
import com.zr.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther zhaorui
 * @date 2019-11-24 - 1:16
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void setCheckItemAndCheckGroup(Integer checkgroupId, Integer[] checkitemIds) {
        if (checkitemIds.length > 0 && checkitemIds != null){
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId",checkgroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckItemAndCheckGroup(map);
            }
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        return  checkGroupDao.findAll();

    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //添加数据之前 首先先添加  checkItem和 checkGroup 的关系
        //首先添加数据
        checkGroupDao.add(checkGroup);
        Integer checkgroupId = checkGroup.getId();
        setCheckItemAndCheckGroup(checkgroupId, checkitemIds);

        /*if (checkitemIds.length > 0 && checkitemIds != null){
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId",checkgroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckItemAndCheckGroup(map);
            }
        }*/
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();

        PageHelper.startPage(currentPage,pageSize); //开启分页
        //查询分页结果(包含查询条件)
        Page<CheckGroup> checkGroupList = checkGroupDao.findPage(queryString);
        //分页总条数  每页记录数据
        List<CheckGroup> rows = checkGroupList.getResult();
        long total = checkGroupList.getTotal();
        //将total 和 rows 装入pageResult中 并返回
        PageResult pageResult = new PageResult(total,rows);
        return pageResult;
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup  = checkGroupDao.findById(id);

        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdByCheckGroupId(Integer id) {
        List<Integer> checkitemIds = checkGroupDao.findCheckItemIdByCheckGroupId(id);
        return checkitemIds;
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //更新 CheckGroup
        checkGroupDao.edit(checkGroup);
        //删除与checkGroupId相关的关联表内容
        checkGroupDao.deleteAssociation(checkGroup.getId());
        Integer checkgroupId = checkGroup.getId();
        //调用本类中的setCheckItemAndCheckGroup方法
        setCheckItemAndCheckGroup(checkgroupId,checkitemIds);
       /* if (checkitemIds.length > 0 && checkitemIds != null){
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId",checkgroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.setCheckItemAndCheckGroup(map);
            }
        }*/
    }


}
